// pages/visitorInfo/index.js
const app = getApp()
var mid;
var sid;
var rid;
var uid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canse: false,
    canUse: wx.canIUse('button.open-type.getUserInfo'),
    hitenTop: false,
    isClose:true,
    isFolded: true,
    windowHeight: '',
    windowWidth: '',
    image:'/images/icon/uscarbgm.png',
    hidden:false,
    userCardInfo: '',
    redPackList:[],
    userBasic: {},
    redPackRand:'',
    messageModalput: true,
    message:'',
    coverImg: {
      hidden: true,
      locked: false,
      top: '0',
      bottom: '0'
    },
    bgmState:false,
    bagImg: [
      '/images/icon/userbgm.png',
      '/images/icon/uscarbgm.png',
      '/images/icon/homer.png',
      '/images/icon/recom.png',
      '/images/icon/recomup.png',
      '/images/icon/msg.png',
      '/images/icon/msgup.png',
      '/images/icon/icon3.png',
      '/images/icon/icon5.png',
      '/images/icon/icon7.png',
      '/images/icon/redtop1.png',
      '/images/icon/redbot1.png',
      '/images/icon/redtop2.png',
      '/images/icon/redbot2.png',
    ],
  },
  change: function (e) {
    this.setData({
      isFolded: !this.data.isFolded,
    })
  },
  getStorage: function () {
    var that = this;
    try {
      const value = wx.getStorageSync('userInfo')
      console.log('用户缓存信息',value)
      if (value) {
        uid = value.uid;
        that.getUserBasicInfo();
        that.getAddBrowse();
        that.getVisitorInfo();
        that.getRedpackRandLet()
      } else {
        that.getUserInfo();
      }
    } catch (e) {
      app.prompt('登陆失败' + e);
      // Do something when catch error
    }
  },

  /**
     * 授权获取用户信息
     */
  getUserInfo: function () {
    console.log('访客ID：'+mid+'分享者ID：'+sid+'红包ID：'+rid)
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          app.getUserInfo(that, mid, sid, rid);
        } else {
          that.setData({
            canse: true
          })
        }
      }
    })
  },
  /**
 * 用户授权
 */
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      that.getUserInfo();
    } else {
      app.prompt('登陆失败');
    }
  },
  //获取个人基本信息
  getUserBasicInfo: function () {
    var that = this;
    var data = [];
    data['uid'] = uid;
    var url = 'We_Community/getBrowsePersonalUserBasic'
    var calkdata = app.netWorkRequest(that, data, url, 'userBasic')
  },

  //联系ta拨号
  dialVisitor: function (e) {
    console.log('传递信息', e)
    var id = e.currentTarget.id;
    wx.makePhoneCall({
      phoneNumber: id, // 仅为示例，并非真实的电话号码
      success: function (res) {

      },
      fail: function (res) {
        console.log(res)
        app.promptModal('拨号失败')
      }
    })
  },
  baseImage: function () {
    var that = this;
    var data = that.data.bagImg;
    console.log('转码', data)
    for (var i = 0; i < data.length; i++) {
      var bgm = app.baseImage(data[i]);
      data[i] = bgm;
    }
    that.setData({
      bagImg: data
    })
  },
  goUserInfo:function(){
    wx.switchTab({
      url: '../userInfo/index'
    })
  },
  goAddRedpag:function(){
    this.setData({ isClose: false });
    wx.switchTab({
      url: '../addRedpag/index'
    })
  },
  goRecRedpag: function () {
    this.setData({ isClose: false });
    var that = this;
    var rid = that.data.redPackRand['rid'];
    wx.navigateTo({
      url: '../recRedpag/index?mid=' + mid+'&rid='+rid,
    })
  },
  goOpenRedpag:function(){
    this.setData({ isClose: false })
    wx.switchTab({
      url: '../openRedpag/index',
    })
  },
  getAddBrowse: function () {
    var that = this;
    var data = [];
    data['mid'] = mid;
    console.log('热点参数', data)
    var url = 'We_Community/getAddBrowseNum';
    var calkdata = app.netWorkRequest(that, data, url,'BrowseNum')
  },
  getAddGiveup:function(){
    var that = this;
    var data = [];
    data['mid'] = mid;
    var url = 'We_Community/getAddGiveUpNum';
    var calkdata = app.netWorkRequest(that, data, url, 'giveUpAdd')
  },
  getDellGiveup: function () {
    var that = this;
    var data = [];
    data['mid'] = mid;
    var url = 'We_Community/getDellGiveUpNum';
    var calkdata = app.netWorkRequest(that, data, url, 'giveUpDell')
  },
  //获取名片信息
  getVisitorInfo() {
    console.log('浏览名片')
    var that = this;
    var data = [];
    data['uid'] = uid;
    data['mid'] = mid;
    var url = 'We_Community/getBrowseOtherUserInfo';
    var calkdata = app.netWorkRequest(that, data, url, 'userCardInfo')
  },
  //获取指定红包被领取记录
  getAppointRedpagList: function (rid){
    var that = this;
    var data = [];
    data['uid'] = uid;
    data['rid'] = rid;
    var url = 'We_Redenvelopes/getAppointRedenvelopeCodeList';
    var calkdata = app.netWorkRequest(that, data, url, 'redPackList')
  },
  //判断是否可领红包
  getRedpackRandLet: function (){
    var that = this;
    var data = [];
    data['uid'] = uid;
    data['mid'] = mid;
    if (sid && sid !== '' && sid != undefined)
    data['sid'] = sid;
    if (rid && rid !== '' && rid != undefined)
    data['rid'] = rid;
    console.log('判断是否可领红包',data)
    var url = 'We_Redenvelopes/getRandomRedenvelopeCodeLet';
    var calkdata = app.netWorkRequest(that, data, url, 'redPackRand')
  },
  //抢红包
  getRedpackReceiveAppoint: function (rid) {
    var that = this;
    var data = [];
    data['uid'] = uid;
    data['mid'] = mid;
    data['rid'] = rid;
    console.log('点击抢红包', data)
    var url = 'We_Redenvelopes/getRandomRedenvelopeCodeLimit';
    var calkdata = app.netWorkRequest(that, data, url, 'redPackRand')
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  messageModalput: function () {
    this.setData({
      messageModalput: !this.data.messageModalput
    })
  },
  messageComplete: function (e) {
    var msg = e.detail.value;
    console.log('留言内容', msg)
    var that = this;
    that.setData({
      message: msg
    })
  },
  previewImage:function(e){
    console.log('预览图片', e)
    var that = this;
    var t = that.data.userCardInfo;
    var current = e.target.dataset.src;
    var data = t['Poster'];
    var list = [];
    for (var i = 0; i < data.length; i++) {
      list.push(data[i]['Url'])
    }
    app.previewImage(current, list);
  },
  //取消按钮  
  cancel: function () {
    console.log('用户id' + mid)
    this.setData({
      messageModalput: true,
      message:''
    });
  },
  //确认  
  confirm: function (e) {
    console.log('用户id' + mid, '留言内容：' + msg)
    var that = this;
    var msg = that.data.message;
    if(!msg || msg == '') 
    return app.promptModal('留言不能为空');
    if (!mid || mid == '')
    return app.prompt('留言失败');
    var data = [];
    data['mid'] = mid;
    data['content'] = msg;
    var url = 'We_Community/getAddMessage';
    var calkdata = app.netWorkRequest(that, data, url, 'addMessage')
    this.setData({
      messageModalput: true,
      message: ''
    })
  },
  vibrateShortTap: function () {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter()
      // 使手机振动15ms
      //wx.vibrateShort();
      wx.vibrateLong();
      console.log('使手机振动15ms')
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      console.log('当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试')
    }
  },
//红包特效
  resetCoverImg: function () {
    this.setData({
      coverImg: {
        hidden: false,
        top: '0',
        bottom: '0'
      }
    })
  },
  bindOpenTap: function () {
    var that = this;
    var rid = that.data.redPackRand['rid'];
    this.getRedpackReceiveAppoint(rid);
    if (!that.data.coverImg.locked) {
      that.setData({ coverImg: { locked: true } });
      var dis = 0;
      var timer = setInterval(function () {
        dis -= 1;
        that.setData({
          coverImg: {
            top: dis + '%',
            bottom: dis + '%'
          }
        })
      }, 10);
      that.vibrateShortTap();
      setTimeout(function () {
        clearInterval(timer);
        //展开首页图
        that.setData({
          coverImg: {
            hidden: true,
            locked: false
          }
        })
        //未展示开屏页直接跳转
        if (that.data.openPage) {
          // that.goToOpenPage();
        } else {
          // that.goToListPage();
        }
      }, 500);
    }
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('Vistor页面取值',options)
    var ht = app.globalData.windowHeight;
    var wt = app.globalData.windowWidth;
    console.log('宽高', ht+'/'+wt)
    this.setData({
      windowHeight: ht,
      windowWidth: wt,
    })
    wx.hideShareMenu();
    mid = options.id;
    sid = options.sid;
    rid = options.rid;
    // this.getStorage();
    // this.getAddBrowse();
    // this.getVisitorInfo();
    // this.getRedpackRandLet()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.baseImage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('访客页面显示')
    if (this.data.isClose) {
      console.log('首次加载')
      this.getStorage();
    }
    this.setData({
      canse: false
    })
    // this.getAddBrowse();
    // this.getVisitorInfo();
    // this.getRedpackRandLet()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    var rid = that.data.redPackRand['rid'];
    that.getAppointRedpagList(rid);
    if (this.data.isClose) {
      console.log('重新打开')
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    console.log('监听页面隐藏')
    setTimeout(function () {
      that.setData({ isClose: true })
    }, 200)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    that.setData({
      hitenTop: true
    })
    var t = that.data;
    var ufo = t.userCardInfo;
    var mfo = t.userBasic;
    var rid = t.redPackRand['rid'];//红包ID
    var sid = mfo['User_id'];//分享者ID
    var mid = ufo['User_id'];//被分享者ID
    var path = 'pages/visitorInfo/index?id=' + mid + '&sid=' + sid+'&rid='+rid;
    var title = mfo['Username'] + '分享了' + ufo['Username'] + '的名片';
    var imageUrl = '';
    console.log('分享信息' + path)
      setTimeout(function () {
        that.setData({
          hitenTop: false
        })
      }, 3000)
    return {
      title: title,
      path: path,
      imageUrl: imageUrl,
    }
  }
})